package com.tanhua.server.controller;

import com.tanhua.model.enums.CommentSubject;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Xiao
 * @date 2021/10/28
 */
@RestController
@RequestMapping("/comments")
public class CommentsController {
    @Autowired
    private CommentsService commentsService;

    /**
     * 圈子-评论列表
     * @return
     */
    @GetMapping
    public ResponseEntity getCommentPage(
            String movementId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = commentsService.getCommentPage(movementId, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 圈子-发布评论
     * @param map
     * @return
     */
    @PostMapping
    public ResponseEntity publish(@RequestBody Map map) {
        String movementId = (String) map.get("movementId");
        String comment = (String) map.get("comment");
        commentsService.publish(movementId, comment, CommentSubject.MOVEMENT);
        return ResponseEntity.ok("评论成功！");
    }

    /**
     * 圈子-单条评论点赞、取消点赞
     * @param id
     * @param dispose
     * @return
     */
    @GetMapping("/{id}/{dispose}")
    public ResponseEntity disposeComment(@PathVariable String id, @PathVariable String dispose) {
        Boolean choose = StringUtils.equals("like", dispose);
        Integer count = commentsService.disposeComment(id, choose, CommentSubject.COMMENT);
        return ResponseEntity.ok(count);
    }
}
